package com.henzil.siji.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;
import android.util.Log;

import com.henzil.siji.bean.VehiclesInfo;

public class DriverContentProvider extends ContentProvider  {

	public static final String PROVIDER_NAME = "com.henzil.cp.drivers";

	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ PROVIDER_NAME + "/driver");
	
	public static final Uri CONTENT_URI_ADD_VEHICLESINFO = Uri.parse("content://"
			+ PROVIDER_NAME + "/addvehiclesinfo");
	
	public static final Uri CONTENT_URI_VEHICLESINFO_DELETE = Uri.parse("content://"
			+ PROVIDER_NAME + "/vehiclesinfo_delete");
	
	public static final Uri CONTENT_URI_UPDATE_VEHICLESINFO = Uri.parse("content://"
			+ PROVIDER_NAME + "/update_vehiclesinfo");
	
	private static final String VEHICLESINFO_TABLE_NAME = "vehiclesinfo";

	public static final String _ID = "_id";
	//车牌号
	public static final String CARLICWNSE = "carLicense";
	//发动机号
	public static final String ENGINEMODEL = "engineModel";
	//图片id
	public static final String IMAGE_ID = "image_id";
	
	private static final int DATABASE_VERSION = 4;

	private static final int ITEMS = 1;

	private static final int ITEM = 2;
	
	private static final int ADDVEHICLESINFO = 3;
	
	private static final int DELETE_VEHICLESINFO = 4;
	
	private static final int UPDATE_VEHICLESINFO = 5;

	private static SQLiteDatabase database;
	
	private static UriMatcher uriMatcher;

	static {
		/**
		 * 设置uri matcher，这里有不明白的见：http://marshal.easymorse.com/archives/2991
		 */
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(PROVIDER_NAME, "driver/#", ITEM);
		uriMatcher.addURI(PROVIDER_NAME, "driver/", ITEMS);
		
		uriMatcher.addURI(PROVIDER_NAME, "addvehiclesinfo", ADDVEHICLESINFO);
		uriMatcher.addURI(PROVIDER_NAME, "vehiclesinfo_delete", DELETE_VEHICLESINFO);
		uriMatcher.addURI(PROVIDER_NAME, "update_vehiclesinfo", UPDATE_VEHICLESINFO);
	}

	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		switch (uriMatcher.match(uri)) {
		case DELETE_VEHICLESINFO:
			/**
			 * 删除数据库内容
			 */
			return database.delete(VEHICLESINFO_TABLE_NAME, selection, selectionArgs);
		default:
			throw new IllegalArgumentException("delete data >>unknown uri: " + uri);
		}
	}

	@Override
	public String getType(Uri arg0) {
		Log.d("list", "uri match getType");
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		switch (uriMatcher.match(uri)) {
		case ADDVEHICLESINFO:
			/**
			 * 向数据库添加
			 */
		
			database.insert(VEHICLESINFO_TABLE_NAME, _ID, values);
			return uri;
		default:
			throw new IllegalArgumentException("insert data >>unknown uri: " + uri);
		}
		
	}

	@Override
	public boolean onCreate() {
		database = new DriverDatabaseHelper(getContext(), VEHICLESINFO_TABLE_NAME, null,
				DATABASE_VERSION).getWritableDatabase();
		return database != null;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = null;
		Log.v("drivers", ";;;;;;;;;;;;;;;;");
		switch (uriMatcher.match(uri)) {// 判断是访问river集合还是指定id访问单条记录
		case ITEMS:
			Log.v("drivers", "~~~~~~~~~~~~~~~");
			cursor = database.query(VEHICLESINFO_TABLE_NAME, projection, selection,
					selectionArgs, null, null, sortOrder);
			Log.v("drivers", "@@@@@@@@@@"+cursor);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			Log.v("drivers", "#########");
			return cursor;
		case ITEM:
			cursor = database.query(VEHICLESINFO_TABLE_NAME, projection, _ID + "="
					+ uri.getPathSegments().get(1), selectionArgs, null, null,
					sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			return cursor;
		default:
			throw new IllegalArgumentException("unknown uri: " + uri);
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int returnValue = -1;
		switch (uriMatcher.match(uri)) {
		case 0:
			returnValue = database.update(VEHICLESINFO_TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			break;
			
		case UPDATE_VEHICLESINFO:
			returnValue = database.update(VEHICLESINFO_TABLE_NAME, values, selection,
					selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
		default:
			break;
		}
		
		return returnValue;
	}

	private static class DriverDatabaseHelper extends SQLiteOpenHelper {

		private List<VehiclesInfo> vehiclesInfos = new ArrayList<VehiclesInfo>();

		public DriverDatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		private void initDriverList() {
//			VehiclesInfo vehiclesInfo = new VehiclesInfo(
//					"EZ2984", "204738");
//			vehiclesInfos.add(vehiclesInfo);
//
//			vehiclesInfo = new VehiclesInfo(
//					"A9G009", "F94144");
//			vehiclesInfos.add(vehiclesInfo);

		}

		@Override
		public void onCreate(SQLiteDatabase database) {
			initDriverList();
			database.execSQL("create table if not exists "+VEHICLESINFO_TABLE_NAME+"("
					+ " _id integer primary key autoincrement," + " carLicense text,"
					+ " engineModel text,"
					+ " image_id integer "
					+ ");");

			SQLiteStatement statement = database
					.compileStatement("insert into "+VEHICLESINFO_TABLE_NAME+"(carLicense,engineModel,image_id) values(?,?,?)");

			for (VehiclesInfo vehiclesInfo : vehiclesInfos) {
				int index = 1;
				statement.bindString(index++, vehiclesInfo.getCarLicense());
				statement.bindString(index++, vehiclesInfo.getEngineModel());
				statement.bindLong(index++, vehiclesInfo.getImageId());
				statement.executeInsert();
			}

			statement.close();
		}

		@Override
		public void onUpgrade(SQLiteDatabase database, int oldVersion,
				int newVersion) {
			database.execSQL("drop table if exists "+VEHICLESINFO_TABLE_NAME);
			onCreate(database);
		}

	}
}
